package com.apollographql.apollo.internal;

import com.apollographql.apollo.ApolloCall;
import com.apollographql.apollo.ApolloQueryWatcher;
import com.apollographql.apollo.api.OperationName;
import com.apollographql.apollo.api.Query;
import com.apollographql.apollo.api.Response;
import com.apollographql.apollo.api.ScalarTypeAdapters;
import com.apollographql.apollo.api.cache.http.HttpCachePolicy;
import com.apollographql.apollo.api.internal.ApolloLogger;
import com.apollographql.apollo.cache.CacheHeaders;
import com.apollographql.apollo.cache.normalized.ApolloStore;
import com.apollographql.apollo.exception.ApolloException;
import com.apollographql.apollo.fetcher.ApolloResponseFetchers;
import com.apollographql.apollo.interceptor.ApolloInterceptor;
import com.apollographql.apollo.interceptor.ApolloInterceptorFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import okhttp3.Call;
import okhttp3.HttpUrl;

/* access modifiers changed from: package-private */
public final class QueryReFetcher {
    private ApolloCallTracker callTracker;
    private final List<RealApolloCall> calls;
    private final AtomicBoolean executed = new AtomicBoolean();
    final ApolloLogger logger;
    OnCompleteCallback onCompleteCallback;
    private List<OperationName> queryWatchers;

    /* access modifiers changed from: package-private */
    public interface OnCompleteCallback {
        void onFetchComplete();
    }

    static Builder builder() {
        return new Builder();
    }

    QueryReFetcher(Builder builder) {
        this.logger = builder.logger;
        this.calls = new ArrayList(builder.queries.size());
        for (Query query : builder.queries) {
            this.calls.add(RealApolloCall.builder().operation(query).serverUrl(builder.serverUrl).httpCallFactory(builder.httpCallFactory).scalarTypeAdapters(builder.scalarTypeAdapters).apolloStore(builder.apolloStore).httpCachePolicy(HttpCachePolicy.NETWORK_ONLY).responseFetcher(ApolloResponseFetchers.NETWORK_ONLY).cacheHeaders(CacheHeaders.NONE).logger(builder.logger).applicationInterceptors(builder.applicationInterceptors).applicationInterceptorFactories(builder.applicationInterceptorFactories).autoPersistedOperationsInterceptorFactory(builder.autoPersistedOperationsInterceptorFactory).tracker(builder.callTracker).dispatcher(builder.dispatcher).build());
        }
        this.queryWatchers = builder.queryWatchers;
        this.callTracker = builder.callTracker;
    }

    /* access modifiers changed from: package-private */
    public void refetch() {
        if (this.executed.compareAndSet(false, true)) {
            refetchQueryWatchers();
            refetchQueries();
            return;
        }
        throw new IllegalStateException("Already Executed");
    }

    /* access modifiers changed from: package-private */
    public void cancel() {
        for (RealApolloCall call : this.calls) {
            call.cancel();
        }
    }

    private void refetchQueryWatchers() {
        try {
            for (OperationName operationName : this.queryWatchers) {
                for (ApolloQueryWatcher queryWatcher : this.callTracker.activeQueryWatchers(operationName)) {
                    queryWatcher.refetch();
                }
            }
        } catch (Exception e) {
            this.logger.e(e, "Failed to re-fetch query watcher", new Object[0]);
        }
    }

    private void refetchQueries() {
        final OnCompleteCallback completeCallback = this.onCompleteCallback;
        final AtomicInteger callsLeft = new AtomicInteger(this.calls.size());
        for (final RealApolloCall call : this.calls) {
            call.enqueue(new ApolloCall.Callback() {
                /* class com.apollographql.apollo.internal.QueryReFetcher.AnonymousClass1 */

                @Override // com.apollographql.apollo.ApolloCall.Callback
                public void onResponse(Response response) {
                    OnCompleteCallback onCompleteCallback;
                    if (callsLeft.decrementAndGet() == 0 && (onCompleteCallback = completeCallback) != null) {
                        onCompleteCallback.onFetchComplete();
                    }
                }

                @Override // com.apollographql.apollo.ApolloCall.Callback
                public void onFailure(ApolloException e) {
                    OnCompleteCallback onCompleteCallback;
                    if (QueryReFetcher.this.logger != null) {
                        QueryReFetcher.this.logger.e(e, "Failed to fetch query: %s", call.operation);
                    }
                    if (callsLeft.decrementAndGet() == 0 && (onCompleteCallback = completeCallback) != null) {
                        onCompleteCallback.onFetchComplete();
                    }
                }
            });
        }
    }

    /* access modifiers changed from: package-private */
    public static final class Builder {
        ApolloStore apolloStore;
        List<ApolloInterceptorFactory> applicationInterceptorFactories;
        List<ApolloInterceptor> applicationInterceptors;
        ApolloInterceptorFactory autoPersistedOperationsInterceptorFactory;
        ApolloCallTracker callTracker;
        Executor dispatcher;
        Call.Factory httpCallFactory;
        ApolloLogger logger;
        List<Query> queries = Collections.emptyList();
        List<OperationName> queryWatchers = Collections.emptyList();
        ScalarTypeAdapters scalarTypeAdapters;
        HttpUrl serverUrl;

        /* access modifiers changed from: package-private */
        public Builder queries(List<Query> queries2) {
            this.queries = queries2 != null ? queries2 : Collections.emptyList();
            return this;
        }

        public Builder queryWatchers(List<OperationName> queryWatchers2) {
            this.queryWatchers = queryWatchers2 != null ? queryWatchers2 : Collections.emptyList();
            return this;
        }

        /* access modifiers changed from: package-private */
        public Builder serverUrl(HttpUrl serverUrl2) {
            this.serverUrl = serverUrl2;
            return this;
        }

        /* access modifiers changed from: package-private */
        public Builder httpCallFactory(Call.Factory httpCallFactory2) {
            this.httpCallFactory = httpCallFactory2;
            return this;
        }

        /* access modifiers changed from: package-private */
        @Deprecated
        public Builder responseFieldMapperFactory(ResponseFieldMapperFactory responseFieldMapperFactory) {
            return this;
        }

        /* access modifiers changed from: package-private */
        public Builder scalarTypeAdapters(ScalarTypeAdapters scalarTypeAdapters2) {
            this.scalarTypeAdapters = scalarTypeAdapters2;
            return this;
        }

        /* access modifiers changed from: package-private */
        public Builder apolloStore(ApolloStore apolloStore2) {
            this.apolloStore = apolloStore2;
            return this;
        }

        /* access modifiers changed from: package-private */
        public Builder dispatcher(Executor dispatcher2) {
            this.dispatcher = dispatcher2;
            return this;
        }

        /* access modifiers changed from: package-private */
        public Builder logger(ApolloLogger logger2) {
            this.logger = logger2;
            return this;
        }

        /* access modifiers changed from: package-private */
        public Builder applicationInterceptors(List<ApolloInterceptor> applicationInterceptors2) {
            this.applicationInterceptors = applicationInterceptors2;
            return this;
        }

        /* access modifiers changed from: package-private */
        public Builder applicationInterceptorFactories(List<ApolloInterceptorFactory> applicationInterceptorFactories2) {
            this.applicationInterceptorFactories = applicationInterceptorFactories2;
            return this;
        }

        /* access modifiers changed from: package-private */
        public Builder autoPersistedOperationsInterceptorFactory(ApolloInterceptorFactory interceptorFactories) {
            this.autoPersistedOperationsInterceptorFactory = interceptorFactories;
            return this;
        }

        /* access modifiers changed from: package-private */
        public Builder callTracker(ApolloCallTracker callTracker2) {
            this.callTracker = callTracker2;
            return this;
        }

        /* access modifiers changed from: package-private */
        public QueryReFetcher build() {
            return new QueryReFetcher(this);
        }

        Builder() {
        }
    }
}
